use actix_web::{HttpResponse, web, HttpRequest};
use rust_admin2_common::{types::SwapData, models::index::{LoginInfo, }, crypt};
use crate::{
    common::{Response, Pool},
    apps::backend::models::Admin,
};

/// 后台默认控制器
pub struct Index {}

impl Index {

    /// 测试请求
    pub async fn test() -> &'static str { 
        "hello, there"
    }

    /// 登录页面
    pub async fn login(req: HttpRequest, posted: web::Json<SwapData>, db: Pool) -> HttpResponse {
        let login_info = if let Ok(v) = crypt::decrypt::<LoginInfo>(&posted) { v } else {
            return Response::error("解密提交数据信息失败");
        };
        dbg!(&login_info);
        match Admin::login(&req, &db, &login_info).await {
            Ok(v) => { Response::encrypt_data(&v) },
            Err(message) => { Response::error(&message.to_string()) }
        }
    }
}
